﻿#ifndef DROPDOWNLISTPANEL_H
#define DROPDOWNLISTPANEL_H

#include "common.h"
#include <vector>

class LabeledComboBox;

struct tagListItem
{
    tagListItem():boxsizer(NULL) {}

    wxBoxSizer *boxsizer;
    std::vector<LabeledComboBox*> items;
};

// 标签+下拉框组合控件
class LabeledComboBox : public wxPanel
{
public:
    LabeledComboBox(wxWindow* parent,
                  const wxString& label,
                  const wxArrayString& choices,
                  wxWindowID id = wxID_ANY)
        : wxPanel(parent, id)
    {
        wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);

        m_label = new wxStaticText(this, wxID_ANY, label);
        m_combo = new wxComboBox(this, wxID_ANY, wxEmptyString,
                               wxDefaultPosition, wxDefaultSize,
                               choices, wxCB_DROPDOWN|wxCB_READONLY);
        m_combo->SetSelection(0);

        sizer->Add(m_label, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 5);
        sizer->Add(m_combo, 1, wxEXPAND);

        // 绑定事件
        m_combo->Bind(wxEVT_COMBOBOX, &LabeledComboBox::OnComboBoxSelected, this);

        SetSizer(sizer);
    }

    inline wxString GetName(void) { return m_label->GetLabel(); }
    wxString GetValue() const { return m_combo->GetValue(); }
    void SetValue(const wxString& value) { m_combo->SetValue(value); }
    wxString getParams(void);
    wxComboBox* GetComboBox() const { return m_combo; }
    inline void setTypeName(wxString tname) { m_typeName = tname; }
    inline wxString getTypeName(void) { return m_typeName; }

    // 事件处理函数
    void OnComboBoxSelected(wxCommandEvent& event)
    {
        wxThreadEvent pevent(DRAPLIST_SELECTED_EVENT);
        pevent.SetString(wxString::Format("[%s]",
                                          //this->getTypeName(),
                                          this->getParams()));
        wxQueueEvent(wxApp::GetMainTopWindow(), pevent.Clone());

        // 如果需要，可以让事件继续传播
        event.Skip();
    }

private:
    wxString m_typeName;
    wxStaticText* m_label;
    wxComboBox* m_combo;
};

class DropDownListPanel : public wxScrolledWindow
{
public:
    DropDownListPanel(wxWindow* parent,
                      wxString typeName,
                      wxWindowID id = wxID_ANY,
                      const wxPoint& pos = wxDefaultPosition,
                      const wxSize& size = wxDefaultSize);
    ~DropDownListPanel();

    /// 添加一个选项到列表中
    void addLabeledComboBox(LabeledComboBox *lc);
    wxString getParams(wxVector<wxString> nodes);
    void setParams(wxString node,wxString state);

private:
    void FitInside();

private:
    wxString m_typeName;
    wxBoxSizer* m_mainSizer;
    std::vector<tagListItem> m_listItems;
};

#endif // DROPDOWNLISTPANEL_H
